import Vue from "vue";
import Router from "vue-router";
import {
	getTokenLocal
} from "@/utils/auth.js"

import Home from '@/views/home/Home.vue'

import {
	Dialog
} from "vant";

Vue.use(Router);

const routes = [{ //临时测试组件用
		path: "/test",
		component: () => import("@/views/test/Test.vue"),
	},

	{ //定义重定向路由
		path: "/",
		redirect: "/home",
		meta: {
			title: "首页"
		}
	},
	{
		path: "/home", //路由的路径
		name: "home", //路由的名字
		component: Home, //路由组件
		meta: {
			title: "首页",
			keepAlive: true //是否缓存
		}
	},

	{ //定义商品列表路由
		path: '/product',
		name: 'product',
		component: () => import( /* webpackChunkName: "product" */ '@/views/product/Product.vue'),
		meta: {
			title: "商品",
			keepAlive: true, //是否缓存
		}
	},

	{ //定义商品详情路由
		path: '/product/detail/:id',
		name: 'productDetail',
		component: () => import( /* webpackChunkName: "product" */ '@/views/product/ProductDetail.vue'),
		meta: {
			title: "商品详情"
		}
	},

	{ //定义购物车路由
		path: '/shopcart',
		name: 'shopcart',
		component: () => import( /* webpackChunkName: "product" */ '@/views/shopcart/Shopcart.vue'),
		meta: {
			title: "购物车",
			alive: true, //是否缓存
			requireAuth: true, //这个路由需要认证
		}
	},

	{ //定义我的页面路由
		path: '/user',
		name: 'user',
		component: () => import( /* webpackChunkName: "user" */ '@/views/user/User.vue'),
		meta: {
			title: "我的",
			alive: true, //是否缓存
		}
	},


	{ //定义登录路由
		path: '/login',
		name: 'login',
		component: () => import( /* webpackChunkName: "user" */ '@/views/user/Login.vue'),
		meta: {
			title: "登录"
		}
	},

	{ //定义注册路由
		path: '/register',
		name: 'register',
		component: () => import( /* webpackChunkName: "user" */ '@/views/user/Register.vue'),
		meta: {
			title: "注册"
		}
	},

	{ //个人资料路由
		path: '/profile',
		name: 'profile',
		component: () => import( /* webpackChunkName: "user" */ '@/views/user/Profile.vue'),
		meta: {
			title: "个人资料"
		}
	},

	{ //修改密码路由
		path: "/user/updatePwd",
		component: () => import( /* webpackChunkName: "user" */ "@/views/user/UpdatePwd.vue"),
		name: "updatePwd",
		meta: {
			title: "修改密码"
		}
	},


	{ //定义订单创建路由
		path: '/order/create',
		name: 'orderCreate',
		component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderCreate.vue'),
		meta: {
			title: "新建订单"
		}
	},
	{ //定义订单列表路由
		path: '/order/list',
		name: 'orderList',
		component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderList.vue'),
		meta: {
			title: "订单列表"
		}
	},
	{ //定义订单明细路由
		path: '/order/detail/:id', //动态路由参数
		props: true, //允许props接收
		name: 'orderDetail',
		component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderDetail.vue'),
		meta: {
			title: "订单详情"
		}
	},

	{ //定义地址列表路由
		path: '/address/list',
		name: 'addressList',
		component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressList.vue'),
		meta: {
			title: "地址列表"
		}
	},

	{ //定义地址编辑路由
		path: '/address/edit/:id', //动态路由参数
		props: true, //允许props接收
		name: 'addressEdit',
		component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressEdit.vue'),
		meta: {
			title: "地址编辑"
		}
	},

	{ //定义新闻列表路由
		path: '/news/list',
		name: 'newsList',
		component: () => import( /* webpackChunkName: "news" */ '@/views/news/NewsList.vue'),
		meta: {
			title: "新闻列表",
		}
	},
	{ //定义新闻详情路由
		path: '/news/detail/:id', //动态路由参数
		props: true, //允许props接收
		name: 'newsDetail',
		component: () => import( /* webpackChunkName: "news" */ '@/views/news/NewsDetail.vue'),
		meta: {
			title: "新闻详情",
		}
	},

	{
		path: "/working",
		component: () => import("@/views/Working.vue"),
		name: "working",
		meta: {
			title: "留言反馈"
		},
	},

	{
		path: "*",
		name: "notFound",
		component: () => import( /* webpackChunkName: "notfound" */ '@/views/NotFound.vue'),
		meta: {
			title: "导航页面不存在"
		},
	},
	{ //定义地址编辑路由
		path: '/address/edit/:id', //动态路由参数
		props: true, //允许props接收
		name: 'addressEdit',
		component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressEdit.vue'),
		meta: {
			title: "地址编辑"
		}
	},
	{
		path: "/contactUs",
		component: () => import("@/views/ContactUs.vue"),
		name: "contactUs",
		meta: {
		title: "联系我们"
		}
	},
];

let router = new Router({
	// 解决路由跳转页面没有置顶
	scrollBehavior(to, from, savedPosition) {
		if (savedPosition) {
			return savedPosition;
		} else {
			return {
				x: 0,
				y: 0,
			};
		}
	},
	routes,
});

//设置全局路由导航守卫
router.beforeEach((to, from, next) => {
	
	if (to.meta.title) {
			document.title = to.meta.title;
	}

	if (to.name === 'login') { // 路由条状到login的，直接放行
		console.log(`${from.name}跳转到login`); // 控制台调试输出
		next(); // 路由继续往下跳转
		return;
	}

	if (!to.meta.requireAuth) { // 路由不需要验证的，直接放行
		console.log(`${from.name}跳转到${to.name},目的路由不需要用户认证`); // 控制台调试输出
		next();
		return;
	}

	// 路由需要验证的	
	const token = getTokenLocal();
	if (!token) { // 如果token不存在的，则跳转登录login
		console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token不存在！强制跳转到login`);

		Dialog.confirm({
			message: "没有Token,需要重新登录吗?",
		}).then(() => {
			next({
				name: 'login',
				params: {
					redirect: to.path
				}
			});
		}).catch(() => {
			router.push({
				name: "home"
			});
		});
		return;
	}

	console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token也存在！`);
	// 其余情况，则路由放行
	next(); //
});


const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err);
};

export default router;
